import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { PaymentEntity } from '@/entity/payment.entity';
import { Repository } from 'typeorm';
import { CreatePaymentDto } from '@/modules/payment/dto/create-payment.dto';
import { UpdatePaymentDto } from '@/modules/payment/dto/update-payment.dto';

@Injectable()
export class PaymentService {
  constructor(
    @InjectRepository(PaymentEntity)
    private readonly paymentRepository: Repository<PaymentEntity>,
  ) {}

  async findAll(): Promise<PaymentEntity[]> {
    return this.paymentRepository.find();
  }

  async findOne(id: number): Promise<PaymentEntity> {
    return this.paymentRepository.findOne({ where: { id } });
  }

  async findByOrderId(orderId: number): Promise<PaymentEntity[]> {
    return this.paymentRepository.find({ where: { orderId } });
  }

  async create(createPaymentDto: CreatePaymentDto): Promise<PaymentEntity> {
    const payment = this.paymentRepository.create(createPaymentDto);
    return this.paymentRepository.save(payment);
  }

  async update(
    id: number,
    updatePaymentDto: UpdatePaymentDto,
  ): Promise<PaymentEntity> {
    const payment = await this.paymentRepository.findOne({ where: { id } });
    return this.paymentRepository.save({ ...payment, ...updatePaymentDto });
  }

  async delete(id: number): Promise<void> {
    await this.paymentRepository.delete(id);
  }
}
